package ysq.xyg.base.extend.ehcache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.recovery.model.po.RecoveryLargeCat;
import ysq.xyg.recovery.model.po.RecoveryMiddleCat;
import ysq.xyg.recovery.model.po.RecoverySmallCat;
import ysq.xyg.recovery.service.RecoveryLargeCatService;
import ysq.xyg.recovery.service.RecoveryMiddleCatService;
import ysq.xyg.recovery.service.RecoverySmallCatService;
import ysq.xyg.shop.model.po.ShopBanner;
import ysq.xyg.shop.model.po.ShopItemCat;
import ysq.xyg.shop.service.ShopBannerService;
import ysq.xyg.shop.service.ShopItemCatService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.vo.SysDictContentVo;
import ysq.xyg.sys.service.SysDictContentService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缓存实现
 *
 * @author taocongcong
 * @create 2019-04-11 17:20
 */
@Service("ehCacheService")
public class EhCacheServiceImpl implements EhCacheService{

    @Autowired
    private RecoveryLargeCatService recoveryLargeCatService;
    @Autowired
    private RecoveryMiddleCatService recoveryMiddleCatService;
    @Autowired
    private RecoverySmallCatService recoverySmallCatService;
    @Autowired
    private ShopItemCatService shopItemCatService;
    @Autowired
    private ShopBannerService shopBannerService;
    @Autowired
    private SysDictContentService sysDictContentService;

    @Override
    @Cacheable(value="cacheRecoveryLargeCat")
    public List<RecoveryLargeCat> selectRecoveryLargeCatList() {
        RecoveryLargeCat recoveryLargeCat = new RecoveryLargeCat();
        recoveryLargeCat.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        return recoveryLargeCatService.select(recoveryLargeCat);
    }

    @Override
    @Cacheable(value="cacheRecoveryMiddleCat")
    public Map<String, List<RecoveryMiddleCat>> selectRecoveryMiddleCatList() {
        // 大分类
        Map<String, List<RecoveryMiddleCat>> map = new HashMap<String, List<RecoveryMiddleCat>>();
        RecoveryLargeCat recoveryLargeCatR = new RecoveryLargeCat();
        recoveryLargeCatR.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        List<RecoveryLargeCat> recoveryLargeCatList = recoveryLargeCatService.select(recoveryLargeCatR);
        if (recoveryLargeCatList != null && recoveryLargeCatList.size() > 0) {
            for (RecoveryLargeCat recoveryLargeCat : recoveryLargeCatList) {
                // map
                RecoveryMiddleCat recoveryMiddleCatRecord = new RecoveryMiddleCat();
                recoveryMiddleCatRecord.setLargeCatId(recoveryLargeCat.getId());
                recoveryMiddleCatRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                List<RecoveryMiddleCat> recoveryMiddleCatList = recoveryMiddleCatService.select(recoveryMiddleCatRecord);
                if (recoveryMiddleCatList != null && recoveryMiddleCatList.size() > 0) {
                    for (RecoveryMiddleCat recoveryMiddleCat : recoveryMiddleCatList) {
                        // 获取
                        RecoverySmallCat recoverySmallCatRecord = new RecoverySmallCat();
                        recoverySmallCatRecord.setMiddleCatId(recoveryMiddleCat.getId());
                        recoverySmallCatRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                        List<RecoverySmallCat> recoverySmallCatList = recoverySmallCatService.select(recoverySmallCatRecord);
                        recoveryMiddleCat.setRecoverySmallCatList(recoverySmallCatList);
                    }
                }
                map.put(recoveryLargeCat.getId(), recoveryMiddleCatList);
            }
        }
        return map;
    }

    @Override
    @Cacheable(value="cacheOnlyRecoveryMiddleCat")
    public Map<String, List<RecoveryMiddleCat>> cacheOnlyRecoveryMiddleCat() {
        List<SysDictContent> dictList=sysDictContentService.acceptList(new SysDictContentVo("unit_type",BaseConstant.BASE_STATUS_NORMAL));

        Map<String, List<RecoveryMiddleCat>> map = new HashMap<String, List<RecoveryMiddleCat>>();
        RecoveryLargeCat recoveryLargeCatR = new RecoveryLargeCat();
        recoveryLargeCatR.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        List<RecoveryLargeCat> recoveryLargeCatList = recoveryLargeCatService.select(recoveryLargeCatR);
        if (recoveryLargeCatList != null && recoveryLargeCatList.size() > 0) {
            for (RecoveryLargeCat recoveryLargeCat : recoveryLargeCatList) {
                // map
                RecoveryMiddleCat recoveryMiddleCatRecord = new RecoveryMiddleCat();
                recoveryMiddleCatRecord.setLargeCatId(recoveryLargeCat.getId());
                recoveryMiddleCatRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                List<RecoveryMiddleCat> recoveryMiddleCatList = recoveryMiddleCatService.select(recoveryMiddleCatRecord);
                if(recoveryMiddleCatList!=null&&recoveryMiddleCatList.size()>0){
                    for(RecoveryMiddleCat recoveryMiddleCat:recoveryMiddleCatList){
                        for(SysDictContent sysDictContent:dictList){
                            if(sysDictContent.getValue().equals(recoveryMiddleCat.getRatePrice())){
                                recoveryMiddleCat.setRatePriceName(sysDictContent.getName());
                            }
                        }
                    }
                }

                map.put(recoveryLargeCat.getId(), recoveryMiddleCatList);
            }
        }
        return map;
    }

    @Override
    @Cacheable(value="cacheRecoverySmallCat")
    public Map<String, List<RecoverySmallCat>> selectRecoverySmallCatList() {
        Map<String, List<RecoverySmallCat>> map = new HashMap<String, List<RecoverySmallCat>>();
        RecoveryMiddleCat recoveryMiddleCatR = new RecoveryMiddleCat();
        recoveryMiddleCatR.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        List<RecoveryMiddleCat> recoveryMiddleCatList = recoveryMiddleCatService.select(recoveryMiddleCatR);
        if (recoveryMiddleCatList != null && recoveryMiddleCatList.size() > 0) {
            for (RecoveryMiddleCat recoveryMiddleCat : recoveryMiddleCatList) {
                // 获取
                RecoverySmallCat recoverySmallCatRecord = new RecoverySmallCat();
                recoverySmallCatRecord.setMiddleCatId(recoveryMiddleCat.getId());
                recoverySmallCatRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                List<RecoverySmallCat> recoverySmallCatList = recoverySmallCatService.select(recoverySmallCatRecord);
                map.put(recoveryMiddleCat.getId(), recoverySmallCatList);
            }
        }
        return map;
    }

    @Override
    @CacheEvict(value={"cacheRecoveryLargeCat", "cacheRecoveryMiddleCat", "cacheOnlyRecoveryMiddleCat", "cacheRecoverySmallCat"}, allEntries=true)
    public void evictRecoveryCat() {

    }

    @Override
    @Cacheable(value="cacheShopItemCat")
    public List<ShopItemCat> selectShopItemCatList() {
        ShopItemCat shopItemCat = new ShopItemCat();
        shopItemCat.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        return shopItemCatService.select(shopItemCat);
    }

    @Override
    @CacheEvict(value={"cacheShopItemCat"}, allEntries=true)
    public void evictShopItemCat() {

    }

    @Override
    @Cacheable(value="cacheShopBanner")
    public List<ShopBanner> selectShopBannerList() {
        ShopBanner shopBanner = new ShopBanner();
        shopBanner.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        return shopBannerService.select(shopBanner);
    }

    @Override
    @CacheEvict(value={"cacheShopBanner"}, allEntries=true)
    public void evictShopBanner() {

    }
}
